﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WGM_Client_Net35V2.HelperClasses;
using WGM_Client_Net35V2.Views;
using WGM_Client_Net35V2.Properties;
using System.Windows.Media.Effects;

namespace WGM_Client_Net35V2.Controls
{
    /// <summary>
    /// Interaction logic for Place.xaml
    /// </summary>
    public partial class Place : UserControl
    {
        private bool _isPasswordProtected;
        private int _retryCount = 0;
        public WGM_Svc.WGMNetwork PlaceData { get; set; }
        public bool UserIsMember { get; set; }
        public string PlaceName { get { return _placeNameTB.Text; } set { _placeNameTB.Text = value; } }
        public string PlacePassword { get { return _placePasswordTB.Password; } set { _placePasswordTB.Password = value; } }
        public string PlaceHeadline { get { return _placeHeadlineTB.Text; } set { _placeHeadlineTB.Text = value; } }
        public string PlaceCreator { get { return _placeCreatorTB.Text; } set { _placeCreatorTB.Text = value; } }
        public bool IsCurrentPlace { get; private set; }
        public bool IsPasswordProtected
        {
            get { return _isPasswordProtected; }
            set
            {
                _isPasswordProtected = value;
                if (value == true && !UserIsMember)
                {
                    _placeLoginPanel.Visibility = Visibility.Visible;
                }
                else
                {
                    _placeLoginPanel.Visibility = Visibility.Collapsed;
                }
            }
        }
        public Place(WGM_Svc.WGMNetwork placeData)
        {
            InitializeComponent();
            SetPlaceData(placeData);
        }

        public void SetPlaceData(WGM_Svc.WGMNetwork placeData)
        {
            MainWindow mainWindow = ClassRegistry.GetClass(typeof(MainWindow)) as MainWindow;
            mainWindow.WGMClient.GetOnlineUserNamesCompleted += new EventHandler<WGM_Client_Net35V2.WGM_Svc.GetOnlineUserNamesCompletedEventArgs>(WGMClient_GetOnlineUserNamesCompleted);
            PlaceName = placeData.NetworkName;
            PlaceHeadline = placeData.NetwordDescription;
            PlaceCreator = placeData.NetworkCreatedBy;
            mainWindow.WGMClient.GetOnlineUserNamesAsync(placeData.NetworkName, placeData.NetworkName);
            PlaceData = placeData;
            UserIsMember = placeData.IsRequesterAMember;
            IsPasswordProtected = placeData.IsPasswordProtected;
            _placeTotalMembersTB.Text = placeData.NetworkMembersCount.ToString();
            if (placeData.IsRequesterAMember)
            {
                _placeColour.Color = Colors.Lime;
            }
            else if (placeData.IsPasswordProtected == false)
            {
                _placeColour.Color = Colors.Yellow;
            }
            else
            {
                _placeColour.Color = Colors.Red;
            }
        }
        public void SetEnabled(bool enabled)
        {
            this.IsEnabled = enabled;
        }
        public void SetCurrentPlace(bool isCurrent)
        {
            IsCurrentPlace = isCurrent;
            if (isCurrent)
            {
                this.Effect = null;
            }
            else
            {
                this.Effect = new BlurEffect();
            }
        }
        void WGMClient_GetOnlineUserNamesCompleted(object sender, WGM_Client_Net35V2.WGM_Svc.GetOnlineUserNamesCompletedEventArgs e)
        {
            MainWindow mainWindow = ClassRegistry.GetClass(typeof(MainWindow)) as MainWindow;

            if ((e.UserState as string) == PlaceName)
            {
                if (e.Error == null)
                {
                    if (e.Result != null)
                        _placeOnlineMembersTB.Text = e.Result.Count.ToString();
                    else
                        _placeOnlineMembersTB.Text = "unkown";
                }
                else
                {
                    _placeOnlineMembersTB.Text = "unkown";
                    _retryCount++;
                    if (_retryCount < 3)
                    {
                        mainWindow.WGMClient.GetOnlineUserNamesAsync(PlaceName, PlaceName);
                        return;
                    }
                }
                mainWindow.WGMClient.GetOnlineUserNamesCompleted -= new EventHandler<WGM_Client_Net35V2.WGM_Svc.GetOnlineUserNamesCompletedEventArgs>(WGMClient_GetOnlineUserNamesCompleted);

            }


        }
        private void OnEnterNetworkClick(object sender, MouseButtonEventArgs e)
        {
            MainWindow mainWindow = ClassRegistry.GetClass(typeof(MainWindow)) as MainWindow;
            mainWindow.ShowAlert("Joining network: " + PlaceName);
            MainView mainView = ClassRegistry.GetClass(typeof(MainView)) as MainView;
            mainView.SetBusy(true);
            mainWindow.WGMClient.JoinNetworkAsync(Settings.Default.LastLoginName, Settings.Default.LastLoginPassword, PlaceName, _placePasswordTB.Password, Settings.Default.LastMacAddress, this);
        }
    }
}
